//
//  NewsNavBarView.m
//  LPSNews
//
//  Created by Ruan on 14-10-10.
//  Copyright (c) 2014年 LPS. All rights reserved.
//

#import "NewsNavBarView.h"
#import "UIButton+NavButton.h"



@implementation NewsNavBarView

-(void)dealloc
{
    
    [_topnewsButton release];
    [_techButton release];
    [_movieButton release];
    [_autosButton release];
    [_sportsButton release];
    [_gamesButton release];
    [_selectedButton release];
    [_newsNavBarScorll release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIColor *backColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:0.95];
        UIImage *leftMoreImage = [UIImage imageNamed:@"navbar_left_more"];
        
        CGFloat newsNavBarW = [UIScreen mainScreen].bounds.size.width;
        CGFloat newsNavBarH = leftMoreImage.size.height;
        
        self.frame = CGRectMake(0, 0, newsNavBarW, newsNavBarH);
        self.backgroundColor = backColor;
        
        
        NSMutableArray *newsNavBarButtonArray = [[NSMutableArray alloc] init];
        
        UIButton *topnewsButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"头条"];
        UIButton *techButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"科技"];
        UIButton *movieButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"电影"];
        UIButton *phoneButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"手机"];
        UIButton *sportsButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"体育"];
        UIButton *gamesButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"游戏"];
        UIButton *digitalButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"数码"];
        UIButton *runButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"跑步"];
        UIButton *fashionButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"时尚"];
        UIButton *societyButton = [UIButton buttonAddNewsNavBarButtonWithTitle:@"家居"];
        
        
        self.topnewsButton = topnewsButton;
        self.techButton = techButton;
        self.movieButton = movieButton;
        self.autosButton = phoneButton;
        self.sportsButton = sportsButton;
        self.gamesButton = gamesButton;
        
        [newsNavBarButtonArray addObject:topnewsButton];
        [newsNavBarButtonArray addObject:techButton];
        [newsNavBarButtonArray addObject:movieButton];
        [newsNavBarButtonArray addObject:phoneButton];
        [newsNavBarButtonArray addObject:sportsButton];
        [newsNavBarButtonArray addObject:gamesButton];
        [newsNavBarButtonArray addObject:digitalButton];
        [newsNavBarButtonArray addObject:runButton];
        [newsNavBarButtonArray addObject:fashionButton];
        [newsNavBarButtonArray addObject:societyButton];
        
        
        self.newsNavBarButtonArray = newsNavBarButtonArray;
        UIScrollView *newsNavBarScorll = [[UIScrollView alloc] init];
        CGFloat newsNavBarScorllW =  newsNavBarW - 20 ;
        CGFloat newsNavBarScorllH = newsNavBarH;
        newsNavBarScorll.contentInset = UIEdgeInsetsMake(0, -5, 0, -20);
        newsNavBarScorll.frame = CGRectMake(10, 0, newsNavBarScorllW, newsNavBarScorllH);
        newsNavBarScorll.showsHorizontalScrollIndicator = NO;
       
        int newsTopButtonW = newsNavBarScorllW / 6;
      
        for (int i = 0; i < newsNavBarButtonArray.count; i++) {
            
            UIButton *button = newsNavBarButtonArray[i];
            button.frame = CGRectMake(newsTopButtonW * i + 10, 0, newsTopButtonW, newsNavBarScorllH);

            [button addTarget:self action:@selector(buttonChange:) forControlEvents:UIControlEventTouchUpInside];
            if (i == 0) {
                [self buttonChange:button];
            }
            [newsNavBarScorll addSubview:button];
            newsNavBarScorll.contentSize = CGSizeMake(newsTopButtonW * i, 0);
        }
         newsNavBarScorll.contentInset = UIEdgeInsetsMake(0, 0, 0, newsTopButtonW *1.3);
        self.newsNavBarScorll = newsNavBarScorll;
        

        // news上方的导航条左侧图片
        
        UIImageView *leftMoreView = [[UIImageView alloc] initWithImage:leftMoreImage];
        leftMoreView.frame = CGRectMake(0, 0, leftMoreImage.size.width, newsNavBarH);
        
        
        // news上方的导航条右侧图片
        UIImage *rightMoreImage = [UIImage imageNamed:@"navbar_right_more"];
        UIImageView *rightMoreView = [[UIImageView alloc] initWithImage:rightMoreImage];
        
        CGFloat rightMoreViewX = newsNavBarW - rightMoreImage.size.width ;
        rightMoreView.frame = CGRectMake(rightMoreViewX,0, rightMoreImage.size.width, rightMoreImage.size.height);
        
        [self addSubview:newsNavBarScorll];
        
        [self  addSubview:leftMoreView];
        [self  addSubview:rightMoreView];
        
        
        [newsNavBarButtonArray release];
        [newsNavBarScorll release];
        [leftMoreView release];
        [rightMoreView release];
        
    }
    return self;
}


- (void)buttonChange:(UIButton *)btn
{
    if (self.selectedButton == btn) {
        return;
    }
    [self newsNavBarbuttonTitlechange:btn];
    NSUInteger index = [self.newsNavBarButtonArray indexOfObject:btn];
    if ([_delegate respondsToSelector:@selector(newsNavBarViewChangeContentWithIndex:)]) {
        [_delegate newsNavBarViewChangeContentWithIndex:index];
    }
  
}

- (void)newsNavBarbuttonchangeWithIndex:(int)index
{
    
    UIButton *btn = self.newsNavBarButtonArray[index];
    
    [self newsNavBarbuttonTitlechange:btn];
    
    if (index == 0 || index == 1) {
        UIButton *firstBtn = self.newsNavBarButtonArray[0];
        self.newsNavBarScorll.contentOffset = CGPointMake(firstBtn.frame.origin.x, 0);
        return;
    }else if (index == self.newsNavBarButtonArray.count -1 )
    {
        UIButton *indexBtn = self.newsNavBarButtonArray[self.newsNavBarButtonArray.count -6];
        self.newsNavBarScorll.contentOffset = CGPointMake(indexBtn.frame.origin.x +indexBtn.frame.size.width * 0.3, 0);
        return;
        
    }else if (index >= self.newsNavBarButtonArray.count -4 )
    {
        UIButton *indexBtn = self.newsNavBarButtonArray[self.newsNavBarButtonArray.count -6];
        self.newsNavBarScorll.contentOffset = CGPointMake(indexBtn.frame.origin.x, 0);
        return;
        
    }

        CGPoint point = self.newsNavBarScorll.contentOffset;
        point.x =  btn.frame.origin.x - btn.frame.size.width * 2;
        self.newsNavBarScorll.contentOffset = point;
}


- (void)newsNavBarbuttonTitlechange:(UIButton *)btn
{
    if (self.selectedButton == btn) {
        return;
    }
    [UIView animateWithDuration:4 animations:^{
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        self.selectedButton.titleLabel.font = [UIFont systemFontOfSize:14];
      
    }];
    self.selectedButton.selected = !self.selectedButton;
    btn.selected = YES;
    self.selectedButton = btn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
